package com.qf.dao.impl;

import com.qf.dao.StageDao;
import com.qf.pojo.Project;
import com.qf.pojo.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName StageDaoImpl
 * @Description TODO
 * @Author 86181
 * @Date 2022/9/2 14:14
 * @Version 1.0
 **/
@Repository("stageDao")
public class StageDaoImpl implements StageDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    public int addStage(Stage stage) {
        int update = jdbcTemplate.update("insert into stage values (?,?,?,?,?,?)", stage.getSid(), stage.getSname()
                , stage.getStarttime(), stage.getEndtime(), stage.getPid(), stage.getUpdatetime());
        return update;
    }

    public List<Stage> findAll() {
        List<Stage> query
                = jdbcTemplate.query("select s.* ,p.pname from stage s left join project p on s.pid=p.pid where p.status=1", new BeanPropertyRowMapper<Stage>(Stage.class));
                    return query;

    }
    public int deleteStage(int pid) {
        int update = jdbcTemplate.update("delete * from stage where pid=?",pid);
        int i=0;
        if (update>0){
            i=jdbcTemplate.update("update project set pid=0 where pid=?",pid);
        }
        return i;
    }
}
